package util

import (
	"log"

	"github.com/hashicorp/consul/api"
)

var Client *api.Client

func init(){
	config := api.DefaultConfig()
	config.Address = "127.0.0.1:8500"

	client, cErr := api.NewClient(config)
	if cErr != nil{
		log.Fatal(cErr)
	}

	Client = client
}

func RegSerrvice(){
	
	reg := api.AgentServiceRegistration{}
	reg.ID = "studentservice"
	reg.Name = "studentservice"
	reg.Address = "127.0.0.1"
	reg.Port = 7777
	reg.Tags = []string{"primary"}
	
	check := api.AgentServiceCheck{}
	check.Interval = "5s"
	check.HTTP = "http://127.0.0.1:7777/health"

	reg.Check = &check

	err := Client.Agent().ServiceRegister(&reg)
	if err != nil{
		log.Fatal(err)
	}
}

func UnRegSerrvice(){
	Client.Agent().ServiceDeregister("studentservice")
}